package com.koushikdutta.async;

import java.io.IOException;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.util.Set;
import java.util.concurrent.Semaphore;

public class SelectorWrapper {
    boolean isWaking;
    private Selector selector;
    Semaphore semaphore = new Semaphore(0);

    public Selector getSelector() {
        return this.selector;
    }

    public SelectorWrapper(Selector selector) {
        this.selector = selector;
    }

    public int selectNow() throws IOException {
        return this.selector.selectNow();
    }

    public void select() throws IOException {
        select(0);
    }

    public void select(long timeout) throws IOException {
        try {
            this.semaphore.drainPermits();
            this.selector.select(timeout);
        } finally {
            this.semaphore.release(Integer.MAX_VALUE);
        }
    }

    public Set<SelectionKey> keys() {
        return this.selector.keys();
    }

    public Set<SelectionKey> selectedKeys() {
        return this.selector.selectedKeys();
    }

    public void close() throws IOException {
        this.selector.close();
    }

    public boolean isOpen() {
        return this.selector.isOpen();
    }

    /* JADX WARNING: inconsistent code. */
    /* Code decompiled incorrectly, please refer to instructions dump. */
    public void wakeupOnce() {
        /*
        r6 = this;
        r1 = 1;
        r2 = 0;
        r3 = r6.semaphore;
        r3 = r3.tryAcquire();
        if (r3 != 0) goto L_0x0012;
    L_0x000a:
        r2 = r6.selector;
        r2.wakeup();
        if (r1 == 0) goto L_0x0014;
    L_0x0011:
        return;
    L_0x0012:
        r1 = r2;
        goto L_0x000a;
    L_0x0014:
        monitor-enter(r6);
        r2 = r6.isWaking;	 Catch:{ all -> 0x001b }
        if (r2 == 0) goto L_0x001e;
    L_0x0019:
        monitor-exit(r6);	 Catch:{ all -> 0x001b }
        goto L_0x0011;
    L_0x001b:
        r2 = move-exception;
        monitor-exit(r6);	 Catch:{ all -> 0x001b }
        throw r2;
    L_0x001e:
        r2 = 1;
        r6.isWaking = r2;	 Catch:{ all -> 0x001b }
        monitor-exit(r6);	 Catch:{ all -> 0x001b }
        r0 = 0;
    L_0x0023:
        r2 = 100;
        if (r0 >= r2) goto L_0x0045;
    L_0x0027:
        r2 = r6.semaphore;	 Catch:{ InterruptedException -> 0x003c }
        r4 = 10;
        r3 = java.util.concurrent.TimeUnit.MILLISECONDS;	 Catch:{ InterruptedException -> 0x003c }
        r2 = r2.tryAcquire(r4, r3);	 Catch:{ InterruptedException -> 0x003c }
        if (r2 == 0) goto L_0x003d;
    L_0x0033:
        monitor-enter(r6);
        r2 = 0;
        r6.isWaking = r2;	 Catch:{ all -> 0x0039 }
        monitor-exit(r6);	 Catch:{ all -> 0x0039 }
        goto L_0x0011;
    L_0x0039:
        r2 = move-exception;
        monitor-exit(r6);	 Catch:{ all -> 0x0039 }
        throw r2;
    L_0x003c:
        r2 = move-exception;
    L_0x003d:
        r2 = r6.selector;	 Catch:{ all -> 0x004e }
        r2.wakeup();	 Catch:{ all -> 0x004e }
        r0 = r0 + 1;
        goto L_0x0023;
    L_0x0045:
        monitor-enter(r6);
        r2 = 0;
        r6.isWaking = r2;	 Catch:{ all -> 0x004b }
        monitor-exit(r6);	 Catch:{ all -> 0x004b }
        goto L_0x0011;
    L_0x004b:
        r2 = move-exception;
        monitor-exit(r6);	 Catch:{ all -> 0x004b }
        throw r2;
    L_0x004e:
        r2 = move-exception;
        monitor-enter(r6);
        r3 = 0;
        r6.isWaking = r3;	 Catch:{ all -> 0x0055 }
        monitor-exit(r6);	 Catch:{ all -> 0x0055 }
        throw r2;
    L_0x0055:
        r2 = move-exception;
        monitor-exit(r6);	 Catch:{ all -> 0x0055 }
        throw r2;
        */
        throw new UnsupportedOperationException("Method not decompiled: com.koushikdutta.async.SelectorWrapper.wakeupOnce():void");
    }
}
